package com.bestcem.xm.user.grpc.service.converter;

import cn.hutool.core.lang.ObjectId;
import com.alibaba.fastjson.JSON;
import com.bestcem.bp.xm.user.grpc.v1.enums.UserEditionEnum;
import com.bestcem.bp.xm.user.grpc.v1.enums.UserIndexEnum;
import com.bestcem.bp.xm.user.grpc.v1.enums.UserStatusEnum;
import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.user.enums.BooleanStatusEnum;
import com.bestcem.xm.user.enums.UserEnum;
import com.bestcem.xm.user.grpc.service.converter.strategy.CommonStrategy;
import com.bestcem.xm.user.grpc.service.converter.strategy.UserEnumStrategy;
import com.bestcem.xm.user.service.dto.user.UserDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nullable;
import java.util.*;

/**
 * grpc与本地参数转换类
 *
 * @author panyun <yun.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/8 16:45
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserUserConverter {

    /**
     * grpc请求入参转换成本地入参 UserCondition to UserDTO
     *
     * @param user
     * @return com.bestcem.xm.user.service.dto.user.UserDTO
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/8 17:05
     */
    public static UserDTO userCondition2Dto(UserCondition user) {
        UserDTO userDTO = new UserDTO();
        if (ObjectId.isValid(user.getId())) {
            userDTO.setUserId(user.getId());
        }
        if (ObjectId.isValid(user.getOrgId())) {
            userDTO.setOrgId(user.getOrgId());
        }
        if (user.hasStatus()) {
            userDTO.setStatus(user.getStatus().getNumber());
        }
        if (user.hasEdition()) {
            userDTO.setEdition(user.getEdition().getNumber());
        }
        if (user.hasIndex()) {
            userDTO.setUserIndex(user.getIndex().getNumber());
        }
        if (StringUtils.isNotBlank(user.getUsername())) {
            userDTO.setUserName(user.getUsername());
        }
        if (StringUtils.isNotBlank(user.getPassword())) {
            userDTO.setPassword(user.getPassword());
        }
        if (StringUtils.isNotBlank(user.getName())) {
            userDTO.setName(user.getName());
        }
        if (StringUtils.isNotBlank(user.getAvatar())) {
            userDTO.setAvatar(user.getAvatar());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            userDTO.setEmail(user.getEmail());
        }
        if (StringUtils.isNotBlank(user.getPhone())) {
            userDTO.setPhone(user.getPhone());
        }
        if (StringUtils.isNotBlank(user.getContent())) {
            userDTO.setContent(user.getContent());
        }
        if (user.hasShowGuide()) {
            userDTO.setShowGuide(user.getShowGuide() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        if (user.hasShowVersion()) {
            userDTO.setShowVersion(user.getShowVersion() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        WxInfo wxInfo = user.getWxInfo();
        if (StringUtils.isNotBlank(wxInfo.getUnionId())) {
            userDTO.setWxUnionid(wxInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(wxInfo.getMiniOpenId())) {
            userDTO.setWxMiniOpenid(wxInfo.getMiniOpenId());
        }
        if (StringUtils.isNotBlank(wxInfo.getOffiOpenId())) {
            userDTO.setWxOffiOpenid(wxInfo.getOffiOpenId());
        }
        if (StringUtils.isNotBlank(wxInfo.getNickname())) {
            userDTO.setWxNickname(wxInfo.getNickname());
        }
        FsInfo fsInfo = user.getFsInfo();
        if (StringUtils.isNotBlank(fsInfo.getOpenId())) {
            userDTO.setFsOpenId(fsInfo.getOpenId());
        }
        if (StringUtils.isNotBlank(fsInfo.getUnionId())) {
            userDTO.setFsUnionId(fsInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(fsInfo.getName())) {
            userDTO.setFsName(fsInfo.getName());
        }
        QyWxInfo qyWxInfo = user.getQyWxInfo();
        if (StringUtils.isNotBlank(qyWxInfo.getUserId())) {
            userDTO.setQyWxUserid(qyWxInfo.getUserId());
        }
        if (StringUtils.isNotBlank(qyWxInfo.getThirdUserId())) {
            userDTO.setQyWxOpenUserid(qyWxInfo.getThirdUserId());
        }
        if (StringUtils.isNotBlank(qyWxInfo.getName())) {
            userDTO.setQyWxName(qyWxInfo.getName());
        }
        DingInfo dingInfo = user.getDingInfo();
        if (StringUtils.isNotBlank(dingInfo.getUserId())) {
            userDTO.setDingUserid(dingInfo.getUserId());
        }
        if (StringUtils.isNotBlank(dingInfo.getUnionId())) {
            userDTO.setDingUnionid(dingInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(dingInfo.getName())) {
            userDTO.setDingName(dingInfo.getName());
        }
        if (StringUtils.isNotBlank(user.getSsoName())) {
            userDTO.setSsoName(user.getSsoName());
        }
        if (StringUtils.isNotBlank(user.getSilentId())) {
            userDTO.setSilentId(user.getSilentId());
        }
        if (user.hasAccept()) {
            userDTO.setAcceptFlag(user.getAccept() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        userDTO.setLastLoginTime(DateUtil.getDate(user.getLastLoginTime().toString()));
        if (StringUtils.isNotBlank(user.getRegisterIp())) {
            userDTO.setRegisterIp(user.getRegisterIp());
        }
        if (user.hasInner()) {
            userDTO.setInnerFlag(user.getInner() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        if (CollectionUtils.isNotEmpty(user.getGuideRecordsList())) {
            userDTO.setShowGuideRecords(user.getGuideRecordsList().toString());
        }
        if (user.hasBiShare()) {
            userDTO.setBiShareFlag(user.getBiShare() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        return userDTO;
    }

    /**
     * 本地参数转grpc响应参数
     *
     * @param userList           user列表
     * @param userUserRoleDTOMap userId对应的roleId列表
     * @return java.util.List<com.bestcem.bp.xm.user.grpc.v1.services.User>
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/9 15:25
     */
    public static List<User> userListToGrpc(List<UserDTO> userList, Map<String, List<String>> userUserRoleDTOMap) {
        ArrayList<User> list = new ArrayList<>();
        for (UserDTO user : userList) {
            List<String> roleIdList = userUserRoleDTOMap.get(user.getUserId());

            WxInfo.Builder wxInfo = WxInfo.newBuilder();
            if (StringUtils.isNotBlank(user.getWxUnionid())) {
                wxInfo.setUnionId(user.getWxUnionid());
            }
            if (StringUtils.isNotBlank(user.getWxMiniOpenid())) {
                wxInfo.setMiniOpenId(user.getWxMiniOpenid());
            }
            if (StringUtils.isNotBlank(user.getWxOffiOpenid())) {
                wxInfo.setOffiOpenId(user.getWxOffiOpenid());
            }
            if (StringUtils.isNotBlank(user.getWxNickname())) {
                wxInfo.setNickname(user.getWxNickname());
            }

            FsInfo.Builder fsInfo = FsInfo.newBuilder();
            if (StringUtils.isNotBlank(user.getFsOpenId())) {
                fsInfo.setOpenId(user.getFsOpenId());
            }
            if (StringUtils.isNotBlank(user.getFsUnionId())) {
                fsInfo.setUnionId(user.getFsUnionId());
            }
            if (StringUtils.isNotBlank(user.getFsName())) {
                fsInfo.setName(user.getFsName());
            }

            QyWxInfo.Builder qyWxInfo = QyWxInfo.newBuilder();
            if (StringUtils.isNotBlank(user.getQyWxUserid())) {
                qyWxInfo.setUserId(user.getQyWxUserid());
            }
            if (StringUtils.isNotBlank(user.getQyWxOpenUserid())) {
                qyWxInfo.setThirdUserId(user.getQyWxOpenUserid());
            }
            if (StringUtils.isNotBlank(user.getQyWxName())) {
                qyWxInfo.setName(user.getQyWxName());
            }

            DingInfo.Builder dingInfo = DingInfo.newBuilder();
            if (StringUtils.isNotBlank(user.getDingUserid())) {
                dingInfo.setUserId(user.getDingUserid());
            }
            if (StringUtils.isNotBlank(user.getDingUnionid())) {
                dingInfo.setUnionId(user.getDingUnionid());
            }
            if (StringUtils.isNotBlank(user.getDingName())) {
                dingInfo.setName(user.getDingName());
            }

            User.Builder userBuilder = User.newBuilder();
            if (StringUtils.isNotBlank(user.getUserId())) {
                userBuilder.setId(user.getUserId());
            }
            if (StringUtils.isNotBlank(user.getOrgId())) {
                userBuilder.setOrgId(user.getOrgId());
            }
            if (StringUtils.isNotBlank(user.getUserName())) {
                userBuilder.setUsername(user.getUserName());
            }
            if (StringUtils.isNotBlank(user.getName())) {
                userBuilder.setName(user.getName());
            }
            if (Objects.nonNull(user.getStatus())) {
                userBuilder.setStatus(UserStatusEnum.UserStatus.forNumber(user.getStatus()));
            }
            if (Objects.nonNull(user.getEdition())) {
                userBuilder.setEdition(UserEditionEnum.UserEdition.forNumber(user.getEdition()));
            }
            if (Objects.nonNull(user.getUserIndex())) {
                userBuilder.setIndex(UserIndexEnum.UserIndex.forNumber(user.getUserIndex()));
            }
            if (StringUtils.isNotBlank(user.getPassword())) {
                userBuilder.setPassword(user.getPassword());
            }
            if (StringUtils.isNotBlank(user.getSalt())) {
                userBuilder.setSalt(user.getSalt());
            }
            if (StringUtils.isNotBlank(user.getAvatar())) {
                userBuilder.setAvatar(user.getAvatar());
            }
            if (StringUtils.isNotBlank(user.getEmail())) {
                userBuilder.setEmail(user.getEmail());
            }
            if (StringUtils.isNotBlank(user.getPhone())) {
                userBuilder.setPhone(user.getPhone());
            }
            userBuilder.addAllRoleIds(Objects.nonNull(roleIdList) ? roleIdList : new ArrayList<>());
            if (StringUtils.isNotBlank(user.getContent())) {
                userBuilder.setContent(user.getContent());
            }
            if (Objects.nonNull(user.getShowGuide())) {
                userBuilder.setShowGuide(UserEnum.USER_YES.getFlag().equals(user.getShowGuide()));
            }
            if (Objects.nonNull(user.getShowVersion())) {
                userBuilder.setShowVersion(UserEnum.USER_YES.getFlag().equals(user.getShowVersion()));
            }
            userBuilder.setWxInfo(wxInfo.build());
            userBuilder.setFsInfo(fsInfo.build());
            userBuilder.setQyWxInfo(qyWxInfo.build());
            userBuilder.setDingInfo(dingInfo.build());
            if (StringUtils.isNotBlank(user.getSsoName())) {
                userBuilder.setSsoName(user.getSsoName());
            }
            if (StringUtils.isNotBlank(user.getSilentId())) {
                userBuilder.setSilentId(user.getSilentId());
            }
            if (Objects.nonNull(user.getAcceptFlag())) {
                userBuilder.setAccept(UserEnum.USER_YES.getFlag().equals(user.getAcceptFlag()));
            }
            if (Objects.nonNull(user.getLastLoginTime())) {
                userBuilder.setLastLoginTime(GrpcDateUtil.getTimestamp(user.getLastLoginTime()));
            }
            if (StringUtils.isNotBlank(user.getRegisterIp())) {
                userBuilder.setRegisterIp(user.getRegisterIp());
            }
            if (Objects.nonNull(user.getInnerFlag())) {
                userBuilder.setInner(UserEnum.USER_YES.getFlag().equals(user.getInnerFlag()));
            }
            if (Objects.nonNull(user.getShowGuideRecords())) {
                userBuilder.addAllGuideRecords(JSON.parseArray(user.getShowGuideRecords(), String.class));
            }
            if (Objects.nonNull(user.getBiShareFlag())) {
                userBuilder.setBiShare((UserEnum.USER_YES.getFlag().equals(user.getBiShareFlag())));
            }
            list.add(userBuilder.build());
        }
        return list;
    }

    private static WxInfo userDTOToWxInfo(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }

        com.bestcem.bp.xm.user.grpc.v1.services.WxInfo.Builder wxInfo = WxInfo.newBuilder();

        if (userDTO.getWxUnionid() != null) {
            wxInfo.setUnionId(userDTO.getWxUnionid());
        }
        if (userDTO.getWxMiniOpenid() != null) {
            wxInfo.setMiniOpenId(userDTO.getWxMiniOpenid());
        }
        if (userDTO.getWxOffiOpenid() != null) {
            wxInfo.setOffiOpenId(userDTO.getWxOffiOpenid());
        }
        if (userDTO.getWxNickname() != null) {
            wxInfo.setNickname(userDTO.getWxNickname());
        }

        return wxInfo.build();
    }

    private static FsInfo userDTOToFsInfo(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }

        com.bestcem.bp.xm.user.grpc.v1.services.FsInfo.Builder fsInfo = FsInfo.newBuilder();

        if (userDTO.getFsOpenId() != null) {
            fsInfo.setOpenId(userDTO.getFsOpenId());
        }
        if (userDTO.getFsUnionId() != null) {
            fsInfo.setUnionId(userDTO.getFsUnionId());
        }
        if (userDTO.getFsName() != null) {
            fsInfo.setName(userDTO.getFsName());
        }

        return fsInfo.build();
    }

    private static QyWxInfo userDTOToQyWxInfo(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }

        com.bestcem.bp.xm.user.grpc.v1.services.QyWxInfo.Builder qyWxInfo = QyWxInfo.newBuilder();

        if (userDTO.getQyWxUserid() != null) {
            qyWxInfo.setUserId(userDTO.getQyWxUserid());
        }
        if (userDTO.getQyWxOpenUserid() != null) {
            qyWxInfo.setThirdUserId(userDTO.getQyWxOpenUserid());
        }
        if (userDTO.getQyWxName() != null) {
            qyWxInfo.setName(userDTO.getQyWxName());
        }

        return qyWxInfo.build();
    }

    private static DingInfo userDTOToDingInfo(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }

        com.bestcem.bp.xm.user.grpc.v1.services.DingInfo.Builder dingInfo = DingInfo.newBuilder();

        if (userDTO.getDingUserid() != null) {
            dingInfo.setUserId(userDTO.getDingUserid());
        }
        if (userDTO.getDingUnionid() != null) {
            dingInfo.setUnionId(userDTO.getDingUnionid());
        }
        if (userDTO.getDingName() != null) {
            dingInfo.setName(userDTO.getDingName());
        }

        return dingInfo.build();
    }

    public static User userDto2Grpc(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }

        com.bestcem.bp.xm.user.grpc.v1.services.User.Builder user = User.newBuilder();

        user.setWxInfo(userDTOToWxInfo(userDTO));
        user.setFsInfo(userDTOToFsInfo(userDTO));
        user.setQyWxInfo(userDTOToQyWxInfo(userDTO));
        user.setDingInfo(userDTOToDingInfo(userDTO));
        if (userDTO.getUserId() != null) {
            user.setId(userDTO.getUserId());
        }
        if (userDTO.getUserName() != null) {
            user.setUsername(userDTO.getUserName());
        }
        if (userDTO.getStatus() != null) {
            user.setStatusValue(userDTO.getStatus());
        }
        if (userDTO.getEdition() != null) {
            user.setEditionValue(userDTO.getEdition());
        }
        if (userDTO.getUserIndex() != null) {
            user.setIndexValue(userDTO.getUserIndex());
        }
        if (userDTO.getShowGuide() != null) {
            user.setShowGuide(CommonStrategy.integerToBoolean(userDTO.getShowGuide()));
        }
        if (userDTO.getShowVersion() != null) {
            user.setShowVersion(CommonStrategy.integerToBoolean(userDTO.getShowVersion()));
        }
        if (userDTO.getAcceptFlag() != null) {
            user.setAccept(CommonStrategy.integerToBoolean(userDTO.getAcceptFlag()));
        }
        if (userDTO.getLastLoginTime() != null) {
            user.setLastLoginTime(CommonStrategy.dateToGrpcTimestamp(userDTO.getLastLoginTime()));
        }
        if (userDTO.getInnerFlag() != null) {
            user.setInner(CommonStrategy.integerToBoolean(userDTO.getInnerFlag()));
        }
        if (userDTO.getBiShareFlag() != null) {
            user.setBiShare(CommonStrategy.integerToBoolean(userDTO.getBiShareFlag()));
        }
        if (userDTO.getOrgId() != null) {
            user.setOrgId(userDTO.getOrgId());
        }
        if (userDTO.getName() != null) {
            user.setName(userDTO.getName());
        }
        if (userDTO.getStatus() != null) {
            user.setStatus(UserEnumStrategy.userStatusEnumConveter(userDTO.getStatus()));
        }
        if (userDTO.getEdition() != null) {
            user.setEdition(UserEnumStrategy.userEditionEnumConveter(userDTO.getEdition()));
        }
        if (userDTO.getUserIndex() != null) {
            user.setIndex(UserEnumStrategy.userIndexEnumConveter(userDTO.getUserIndex()));
        }
        if (userDTO.getPassword() != null) {
            user.setPassword(userDTO.getPassword());
        }
        if (userDTO.getSalt() != null) {
            user.setSalt(userDTO.getSalt());
        }
        if (userDTO.getAvatar() != null) {
            user.setAvatar(userDTO.getAvatar());
        }
        if (userDTO.getEmail() != null) {
            user.setEmail(userDTO.getEmail());
        }
        if (userDTO.getPhone() != null) {
            user.setPhone(userDTO.getPhone());
        }
        if (userDTO.getContent() != null) {
            user.setContent(userDTO.getContent());
        }
        if (userDTO.getSsoName() != null) {
            user.setSsoName(userDTO.getSsoName());
        }
        if (userDTO.getSilentId() != null) {
            user.setSilentId(userDTO.getSilentId());
        }
        if (userDTO.getShowGuideRecords() != null) {
            user.addAllGuideRecords(JSON.parseArray(userDTO.getShowGuideRecords(), String.class));
        }
        if (userDTO.getRegisterIp() != null) {
            user.setRegisterIp(userDTO.getRegisterIp());
        }

        return user.build();
    }

    public static List<User> userDtos2Grpcs(List<UserDTO> userDTOList) {
        if (userDTOList == null) {
            return Collections.emptyList();
        }

        List<User> list = new ArrayList<>(userDTOList.size());
        for (UserDTO userDTO : userDTOList) {
            list.add(userDto2Grpc(userDTO));
        }

        return list;
    }

    /**
     * openUser2UserDTO
     *
     * @param user
     * @return com.bestcem.xm.user.service.dto.user.UserDTO
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/06/30 13:53
     */
    public static UserDTO openUser2UserDTO(OpenUser user) {
        UserDTO userDTO = new UserDTO();
        if (ObjectId.isValid(user.getId())) {
            userDTO.setUserId(user.getId());
        }
        if (ObjectId.isValid(user.getOrgId())) {
            userDTO.setOrgId(user.getOrgId());
        }
        if (StringUtils.isNotBlank(user.getUsername())) {
            userDTO.setUserName(user.getUsername());
        }
        if (StringUtils.isNotBlank(user.getPassword())) {
            userDTO.setPassword(user.getPassword());
        }
        if (StringUtils.isNotBlank(user.getSalt())) {
            userDTO.setSalt(user.getSalt());
        }
        if (StringUtils.isNotBlank(user.getName())) {
            userDTO.setName(user.getName());
        }
        if (StringUtils.isNotBlank(user.getAvatar())) {
            userDTO.setAvatar(user.getAvatar());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            userDTO.setEmail(user.getEmail());
        }
        if (StringUtils.isNotBlank(user.getPhone())) {
            userDTO.setPhone(user.getPhone());
        }
        if (StringUtils.isNotBlank(user.getContent())) {
            userDTO.setContent(user.getContent());
        }
        if (user.hasStatus()) {
            userDTO.setStatus(user.getStatusValue());
        }
        if (user.hasEdition()) {
            userDTO.setEdition(user.getEditionValue());
        }
        if (user.hasShowGuide()) {
            userDTO.setShowGuide(user.getShowGuide() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        if (user.hasShowVersion()) {
            userDTO.setShowVersion(user.getShowVersion() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        WxInfo wxInfo = user.getWxInfo();
        if (StringUtils.isNotBlank(wxInfo.getUnionId())) {
            userDTO.setWxUnionid(wxInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(wxInfo.getMiniOpenId())) {
            userDTO.setWxMiniOpenid(wxInfo.getMiniOpenId());
        }
        if (StringUtils.isNotBlank(wxInfo.getOffiOpenId())) {
            userDTO.setWxOffiOpenid(wxInfo.getOffiOpenId());
        }
        if (StringUtils.isNotBlank(wxInfo.getNickname())) {
            userDTO.setWxNickname(wxInfo.getNickname());
        }
        FsInfo fsInfo = user.getFsInfo();
        if (StringUtils.isNotBlank(fsInfo.getOpenId())) {
            userDTO.setFsOpenId(fsInfo.getOpenId());
        }
        if (StringUtils.isNotBlank(fsInfo.getUnionId())) {
            userDTO.setFsUnionId(fsInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(fsInfo.getName())) {
            userDTO.setFsName(fsInfo.getName());
        }
        QyWxInfo qyWxInfo = user.getQyWxInfo();
        if (StringUtils.isNotBlank(qyWxInfo.getUserId())) {
            userDTO.setQyWxUserid(qyWxInfo.getUserId());
        }
        if (StringUtils.isNotBlank(qyWxInfo.getUserId())) {
            userDTO.setQyWxOpenUserid(qyWxInfo.getUserId());
        }
        if (StringUtils.isNotBlank(qyWxInfo.getName())) {
            userDTO.setQyWxName(qyWxInfo.getName());
        }
        DingInfo dingInfo = user.getDingInfo();
        if (StringUtils.isNotBlank(dingInfo.getUserId())) {
            userDTO.setDingUserid(dingInfo.getUserId());
        }
        if (StringUtils.isNotBlank(dingInfo.getUnionId())) {
            userDTO.setDingUnionid(dingInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(dingInfo.getName())) {
            userDTO.setDingName(dingInfo.getName());
        }
        if (StringUtils.isNotBlank(user.getSsoName())) {
            userDTO.setSsoName(user.getSsoName());
        }
        if (StringUtils.isNotBlank(user.getSilentId())) {
            userDTO.setSilentId(user.getSilentId());
        }
        if (user.hasAccept()) {
            userDTO.setAcceptFlag(user.getAccept() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        userDTO.setLastLoginTime(DateUtil.getDate(user.getLastLoginTime().toString()));
        if (StringUtils.isNotBlank(user.getRegisterIp())) {
            userDTO.setRegisterIp(user.getRegisterIp());
        }
        if (user.hasInner()) {
            userDTO.setInnerFlag(user.getInner() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        if (user.hasIndex()) {
            userDTO.setUserIndex(user.getIndexValue());
        }
        if (user.hasBiShare()) {
            userDTO.setBiShareFlag(user.getBiShare() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        return userDTO;
    }

    /**
     * grpc请求入参转换成本地入参  OperateUser to UserDTO
     *
     * @param user
     * @return com.bestcem.xm.user.service.dto.user.UserDTO
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/6/30 16:18
     */
    public static UserDTO operateUser2Dto(OperateUser user) {
        UserDTO userDTO = new UserDTO();
        if (ObjectId.isValid(user.getOrgId())) {
            userDTO.setOrgId(user.getOrgId());
        }
        if (user.hasStatus()) {
            userDTO.setStatus(user.getStatus().getNumber());
        }
        if (user.hasEdition()) {
            userDTO.setEdition(user.getEdition().getNumber());
        }
        if (user.hasIndex()) {
            userDTO.setUserIndex(user.getIndex().getNumber());
        }
        if (StringUtils.isNotBlank(user.getUsername())) {
            userDTO.setUserName(user.getUsername());
        }
        if (StringUtils.isNotBlank(user.getPassword())) {
            userDTO.setPassword(user.getPassword());
        }
        if (StringUtils.isNotBlank(user.getName())) {
            userDTO.setName(user.getName());
        }
        if (StringUtils.isNotBlank(user.getAvatar())) {
            userDTO.setAvatar(user.getAvatar());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            userDTO.setEmail(user.getEmail());
        }
        if (StringUtils.isNotBlank(user.getPhone())) {
            userDTO.setPhone(user.getPhone());
        }
        if (StringUtils.isNotBlank(user.getContent())) {
            userDTO.setContent(user.getContent());
        }
        if (user.hasShowGuide()) {
            userDTO.setShowGuide(user.getShowGuide() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        if (user.hasShowVersion()) {
            userDTO.setShowVersion(user.getShowVersion() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        WxInfo wxInfo = user.getWxInfo();
        if (StringUtils.isNotBlank(wxInfo.getUnionId())) {
            userDTO.setWxUnionid(wxInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(wxInfo.getMiniOpenId())) {
            userDTO.setWxMiniOpenid(wxInfo.getMiniOpenId());
        }
        if (StringUtils.isNotBlank(wxInfo.getOffiOpenId())) {
            userDTO.setWxOffiOpenid(wxInfo.getOffiOpenId());
        }
        if (StringUtils.isNotBlank(wxInfo.getNickname())) {
            userDTO.setWxNickname(wxInfo.getNickname());
        }
        FsInfo fsInfo = user.getFsInfo();
        if (StringUtils.isNotBlank(fsInfo.getOpenId())) {
            userDTO.setFsOpenId(fsInfo.getOpenId());
        }
        if (StringUtils.isNotBlank(fsInfo.getUnionId())) {
            userDTO.setFsUnionId(fsInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(fsInfo.getName())) {
            userDTO.setFsName(fsInfo.getName());
        }
        QyWxInfo qyWxInfo = user.getQyWxInfo();
        if (StringUtils.isNotBlank(qyWxInfo.getUserId())) {
            userDTO.setQyWxUserid(qyWxInfo.getUserId());
        }
        if (StringUtils.isNotBlank(qyWxInfo.getThirdUserId())) {
            userDTO.setQyWxOpenUserid(qyWxInfo.getThirdUserId());
        }
        if (StringUtils.isNotBlank(qyWxInfo.getName())) {
            userDTO.setQyWxName(qyWxInfo.getName());
        }
        DingInfo dingInfo = user.getDingInfo();
        if (StringUtils.isNotBlank(dingInfo.getUserId())) {
            userDTO.setDingUserid(dingInfo.getUserId());
        }
        if (StringUtils.isNotBlank(dingInfo.getUserId())) {
            userDTO.setDingUserid(dingInfo.getUserId());
        }
        if (StringUtils.isNotBlank(dingInfo.getUnionId())) {
            userDTO.setDingUnionid(dingInfo.getUnionId());
        }
        if (StringUtils.isNotBlank(dingInfo.getName())) {
            userDTO.setDingName(dingInfo.getName());
        }
        if (StringUtils.isNotBlank(user.getSsoName())) {
            userDTO.setSsoName(user.getSsoName());
        }
        if (StringUtils.isNotBlank(user.getSilentId())) {
            userDTO.setSilentId(user.getSilentId());
        }
        if (user.hasAccept()) {
            userDTO.setAcceptFlag(user.getAccept() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        userDTO.setLastLoginTime(DateUtil.getDate(user.getLastLoginTime().toString()));
        if (StringUtils.isNotBlank(user.getRegisterIp())) {
            userDTO.setRegisterIp(user.getRegisterIp());
        }
        if (user.hasInner()) {
            userDTO.setInnerFlag(user.getInner() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        if (CollectionUtils.isNotEmpty(user.getGuideRecordsList())) {
            userDTO.setShowGuideRecords(user.getGuideRecordsList().toString());
        }
        if (user.hasBiShare()) {
            userDTO.setBiShareFlag(user.getBiShare() ? UserEnum.USER_YES.getFlag() : UserEnum.USER_NO.getFlag());
        }
        return userDTO;
    }

    /**
     * userDTO to User
     *
     * @param userDTO
     * @return 用户msg
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 18:45 2022/6/30
     */
    public static User dto2Msg(UserDTO userDTO) {
        User.Builder builder = User.newBuilder();
        String userId = userDTO.getUserId();
        String userName = userDTO.getUserName();
        String password = userDTO.getPassword();
        String salt = userDTO.getSalt();
        String name = userDTO.getName();
        String avatar = userDTO.getAvatar();
        String email = userDTO.getEmail();
        String phone = userDTO.getPhone();
        Integer status = userDTO.getStatus();
        String content = userDTO.getContent();
        Integer userIndex = userDTO.getUserIndex();
        Integer edition = userDTO.getEdition();
        Integer showGuide = userDTO.getShowGuide();
        String showGuideRecords = userDTO.getShowGuideRecords();
        Integer showVersion = userDTO.getShowVersion();
        Integer acceptFlag = userDTO.getAcceptFlag();
        Integer innerFlag = userDTO.getInnerFlag();
        Integer biShareFlag = userDTO.getBiShareFlag();
        String registerIp = userDTO.getRegisterIp();
        Date lastLoginTime = userDTO.getLastLoginTime();
        String wxUnionid = userDTO.getWxUnionid();
        String wxMiniOpenid = userDTO.getWxMiniOpenid();
        String wxOffiOpenid = userDTO.getWxOffiOpenid();
        String wxNickname = userDTO.getWxNickname();
        String qyWxUserid = userDTO.getQyWxUserid();
        String qyWxOpenUserid = userDTO.getQyWxOpenUserid();
        String qyWxName = userDTO.getQyWxName();
        String fsUnionId = userDTO.getFsUnionId();
        String fsOpenId = userDTO.getFsOpenId();
        String fsName = userDTO.getFsName();
        String dingUserid = userDTO.getDingUserid();
        String dingUnionid = userDTO.getDingUnionid();
        String dingName = userDTO.getDingName();
        String ssoName = userDTO.getSsoName();
        String silentId = userDTO.getSilentId();
        String orgId = userDTO.getOrgId();

        if (Objects.nonNull(userId)) {
            builder.setId(userId);
        }
        if (Objects.nonNull(userName)) {
            builder.setUsername(userName);
        }
        if (Objects.nonNull(password)) {
            builder.setPassword(password);
        }
        if (Objects.nonNull(salt)) {
            builder.setSalt(salt);
        }
        if (Objects.nonNull(name)) {
            builder.setName(name);
        }
        if (Objects.nonNull(avatar)) {
            builder.setAvatar(avatar);
        }
        if (Objects.nonNull(email)) {
            builder.setEmail(email);
        }
        if (Objects.nonNull(phone)) {
            builder.setPhone(phone);
        }
        if (Objects.nonNull(status)) {
            builder.setStatusValue(status);
        }
        if (Objects.nonNull(content)) {
            builder.setContent(content);
        }
        if (Objects.nonNull(userIndex)) {
            builder.setIndexValue(userIndex);
        }
        if (Objects.nonNull(edition)) {
            builder.setEditionValue(edition);
        }
        if (Objects.nonNull(showGuide)) {
            builder.setShowGuide(BooleanStatusEnum.toBoolean(showGuide));
        }
        if (Objects.nonNull(showGuideRecords)) {
            builder.addAllGuideRecords(JSON.parseArray(showGuideRecords, String.class));
        }
        if (Objects.nonNull(showVersion)) {
            builder.setShowVersion(BooleanStatusEnum.toBoolean(showVersion));
        }
        if (Objects.nonNull(acceptFlag)) {
            builder.setAccept(BooleanStatusEnum.toBoolean(acceptFlag));
        }
        if (Objects.nonNull(innerFlag)) {
            builder.setInner(BooleanStatusEnum.toBoolean(innerFlag));
        }
        if (Objects.nonNull(biShareFlag)) {
            builder.setBiShare(BooleanStatusEnum.toBoolean(biShareFlag));
        }
        if (Objects.nonNull(registerIp)) {
            builder.setRegisterIp(registerIp);
        }
        if (Objects.nonNull(lastLoginTime)) {
            builder.setLastLoginTime(GrpcDateUtil.getTimestamp(lastLoginTime));
        }

        // 微信相关信息
        WxInfo.Builder wxInfoBuilder = WxInfo.newBuilder();
        if (Objects.nonNull(wxUnionid)) {
            wxInfoBuilder.setUnionId(wxUnionid);
        }
        if (Objects.nonNull(wxMiniOpenid)) {
            wxInfoBuilder.setMiniOpenId(wxMiniOpenid);
        }
        if (Objects.nonNull(wxOffiOpenid)) {
            wxInfoBuilder.setOffiOpenId(wxOffiOpenid);
        }
        if (Objects.nonNull(wxNickname)) {
            wxInfoBuilder.setNickname(wxNickname);
        }
        builder.setWxInfo(wxInfoBuilder.build());

        // 企业微信相关信息
        QyWxInfo.Builder qyWxInfoBuilder = QyWxInfo.newBuilder();
        if (Objects.nonNull(qyWxUserid)) {
            qyWxInfoBuilder.setUserId(qyWxUserid);
        }
        if (StringUtils.isNotBlank(qyWxOpenUserid)) {
            qyWxInfoBuilder.setThirdUserId(qyWxOpenUserid);
        }
        if (StringUtils.isNotBlank(qyWxName)) {
            qyWxInfoBuilder.setName(qyWxName);
        }
        builder.setQyWxInfo(qyWxInfoBuilder.build());

        // 飞书相关信息
        FsInfo.Builder fsInfoBuilder = FsInfo.newBuilder();
        if (Objects.nonNull(fsUnionId)) {
            fsInfoBuilder.setUnionId(fsUnionId);
        }
        if (Objects.nonNull(fsOpenId)) {
            fsInfoBuilder.setOpenId(fsOpenId);
        }
        if (Objects.nonNull(fsName)) {
            fsInfoBuilder.setName(fsName);
        }
        builder.setFsInfo(fsInfoBuilder.build());

        // 钉钉相关信息
        DingInfo.Builder dingBuilder = DingInfo.newBuilder();
        if (Objects.nonNull(dingUserid)) {
            dingBuilder.setUserId(dingUserid);
        }
        if (Objects.nonNull(dingUnionid)) {
            dingBuilder.setUnionId(dingUnionid);
        }
        if (Objects.nonNull(dingName)) {
            dingBuilder.setName(dingName);
        }
        builder.setDingInfo(dingBuilder.build());

        if (Objects.nonNull(ssoName)) {
            builder.setSsoName(ssoName);
        }
        if (Objects.nonNull(silentId)) {
            builder.setSilentId(silentId);
        }
        if (Objects.nonNull(orgId)) {
            builder.setOrgId(orgId);
        }
        return builder.build();
    }

    /**
     * 开放平台更新用户
     *
     * @param newUser
     * @return com.bestcem.xm.user.service.dto.user.UserDTO
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/06/30 13:53
     */
    public static UserDTO openUserDTO2UserDTO(UserDTO newUser, UserDTO oldUser) {

        if (ObjectId.isValid(newUser.getOrgId())) {
            oldUser.setOrgId(newUser.getOrgId());
        }
        if (StringUtils.isNotBlank(newUser.getUserName())) {
            oldUser.setUserName(newUser.getUserName());
        }
        if (StringUtils.isNotBlank(newUser.getPassword())) {
            oldUser.setPassword(newUser.getPassword());
        }
        if (StringUtils.isNotBlank(newUser.getSalt())) {
            oldUser.setSalt(newUser.getSalt());
        }
        if (StringUtils.isNotBlank(newUser.getName())) {
            oldUser.setName(newUser.getName());
        }
        if (StringUtils.isNotBlank(newUser.getAvatar())) {
            oldUser.setAvatar(newUser.getAvatar());
        }
        if (StringUtils.isNotBlank(newUser.getEmail())) {
            oldUser.setEmail(newUser.getEmail());
        }
        if (StringUtils.isNotBlank(newUser.getPhone())) {
            oldUser.setPhone(newUser.getPhone());
        }
        if (StringUtils.isNotBlank(newUser.getContent())) {
            oldUser.setContent(newUser.getContent());
        }
        if (Objects.nonNull(newUser.getStatus())) {
            oldUser.setStatus(newUser.getStatus());
        }
        if (Objects.nonNull(newUser.getEdition())) {
            oldUser.setEdition(newUser.getEdition());
        }
        if (Objects.nonNull(newUser.getShowGuide())) {
            oldUser.setShowGuide(newUser.getShowGuide());
        }
        if (Objects.nonNull(newUser.getShowVersion())) {
            oldUser.setShowVersion(newUser.getShowVersion());
        }

        if (StringUtils.isNotBlank(newUser.getWxUnionid())) {
            oldUser.setWxUnionid(newUser.getWxUnionid());
        }
        if (StringUtils.isNotBlank(newUser.getWxMiniOpenid())) {
            oldUser.setWxMiniOpenid(newUser.getWxMiniOpenid());
        }
        if (StringUtils.isNotBlank(newUser.getWxOffiOpenid())) {
            oldUser.setWxOffiOpenid(newUser.getWxOffiOpenid());
        }
        if (StringUtils.isNotBlank(newUser.getWxNickname())) {
            oldUser.setWxNickname(newUser.getWxNickname());
        }
        if (StringUtils.isNotBlank(newUser.getFsOpenId())) {
            oldUser.setFsOpenId(newUser.getFsOpenId());
        }
        if (StringUtils.isNotBlank(newUser.getFsUnionId())) {
            oldUser.setFsUnionId(newUser.getFsUnionId());
        }
        if (StringUtils.isNotBlank(newUser.getFsName())) {
            oldUser.setFsName(newUser.getFsName());
        }

        if (StringUtils.isNotBlank(newUser.getQyWxUserid())) {
            oldUser.setQyWxUserid(newUser.getQyWxUserid());
        }
        if (StringUtils.isNotBlank(newUser.getQyWxOpenUserid())) {
            oldUser.setQyWxOpenUserid(newUser.getQyWxOpenUserid());
        }
        if (StringUtils.isNotBlank(newUser.getQyWxName())) {
            oldUser.setQyWxName(newUser.getQyWxName());
        }

        if (StringUtils.isNotBlank(newUser.getDingUserid())) {
            oldUser.setDingUserid(newUser.getDingUserid());
        }
        if (StringUtils.isNotBlank(newUser.getDingUnionid())) {
            oldUser.setDingUnionid(newUser.getDingUnionid());
        }
        if (StringUtils.isNotBlank(newUser.getDingName())) {
            oldUser.setDingName(newUser.getDingName());
        }
        if (StringUtils.isNotBlank(newUser.getSsoName())) {
            oldUser.setSsoName(newUser.getSsoName());
        }
        if (StringUtils.isNotBlank(newUser.getSilentId())) {
            oldUser.setSilentId(newUser.getSilentId());
        }
        if (Objects.nonNull(newUser.getAcceptFlag())) {
            oldUser.setAcceptFlag(newUser.getAcceptFlag());
        }
        if (StringUtils.isNotBlank(newUser.getRegisterIp())) {
            oldUser.setRegisterIp(newUser.getRegisterIp());
        }
        if (Objects.nonNull(newUser.getInnerFlag())) {
            oldUser.setInnerFlag(newUser.getInnerFlag());
        }
        if (Objects.nonNull(newUser.getUserIndex())) {
            oldUser.setUserIndex(newUser.getUserIndex());
        }
        if (Objects.nonNull(newUser.getBiShareFlag())) {
            oldUser.setBiShareFlag(newUser.getBiShareFlag());
        }
        return oldUser;
    }

    /**
     * UserDTO to UserInfo
     *
     * @param userDTO UserDTO
     * @return UserInfo
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/7/6 13:48
     */
    @Nullable
    public static UserInfo userDtoToUserInfo(UserDTO userDTO) {
        if (Objects.isNull(userDTO)) {
            return null;
        }
        UserInfo.Builder builder = UserInfo.newBuilder();
        if (Objects.nonNull(userDTO.getUserId())) {
            builder.setId(userDTO.getUserId());
        }
        if (Objects.nonNull(userDTO.getOrgId())) {
            builder.setOrgId(userDTO.getOrgId());
        }
        if (Objects.nonNull(userDTO.getUserName())) {
            builder.setUsername(userDTO.getUserName());
        }
        if (Objects.nonNull(userDTO.getName())) {
            builder.setName(userDTO.getName());
        }
        if (Objects.nonNull(userDTO.getAvatar())) {
            builder.setAvatar(userDTO.getAvatar());
        }
        if (Objects.nonNull(userDTO.getEmail())) {
            builder.setEmail(userDTO.getEmail());
        }
        if (Objects.nonNull(userDTO.getPhone())) {
            builder.setPhone(userDTO.getPhone());
        }
        return builder.buildPartial();
    }
}
